package com.itheima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.pojo.Result;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账户控制层:扣款逻辑
 * 接收用户请求,以及返回用户数据,调用service方法
 */
@RestController
@RequestMapping("/bank/account")
public class Controller {

    @Autowired
    private AccountService service;

    @GetMapping("/cost.do")
    public Result takeOut(String userAccount, Integer cost) {

        if (userAccount == null || userAccount == "" || cost == null || cost <= 0) {
            return new Result(false, "参数异常,请重新输入", null);
        }

        boolean flag = service.deduct(userAccount, cost);
        //判断是否扣款成功
        if (flag == true) {
            return new Result(true, "扣款成功.....", userAccount);
        }
        return new Result(false, "扣款失败", null);
    }
}
